#! /bin/sh

repo=$1
url=$2
keyfile=$3

[ -d `dirname $repo` ] && [ -f $keyfile ] || {
	echo "usage: $0 repodir bkurl keyfile" >&2
	exit 1
}

key=`tail -1 $keyfile`

if [ -d $repo ]; then
	cd $repo
	if [ "`bk prs -hnd:KEY: -r+ ChangeSet`" != "$key" ]; then
		bk pull $url || exit 1
		bk undo -fa"$key" || exit 1
	fi
else
	bk clone -r"$key" $url $repo || exit 1
fi
